package com.yygo.util;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by tiansha on 2015/12/7.
 */
/*{
        "resultcode":"200",
        "reason":"Return Successd!",
        "result":{
        "province":"上海",
        "city":"上海",
        "areacode":"021",
        "zip":"200000",
        "company":"中国移动",
        "card":"移动全球通卡"
        },
        "error_code":0
        }*/
public class MobileAddressUtil {

    public static final String URL = "http://apis.juhe.cn/mobile/get?key=34d2642a5b14fab6f4f1c5a03a214228&phone=";

    public static void main(String[] args) throws Exception {
        System.out.println(getAddress("13916109215"));
    }

    public static String getAddress(String number) {

        URL u;
        InputStream is = null;
        DataInputStream dis;
        String s;

        try {
            String url = URL + number;
            /*URLConnection connection = (URLConnection) new URL(url).openConnection();
            connection.setDoOutput(true);
            InputStream os = connection.getInputStream();
            Thread.sleep(100);
            int length = os.available();
            byte[] buff = new byte[length];
            os.read(buff);
            String jsonStr = new String(buff, "gbk");
            System.out.println(jsonStr);
            ObjectMapper mapper = new ObjectMapper();
            ReturnResult returnResult = mapper.readValue(jsonStr, ReturnResult.class);*/

            String inputLine;

            u = new URL(url);
            URLConnection conn = u.openConnection();
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));

            StringBuffer buffer = new StringBuffer();
            while ((inputLine = br.readLine()) != null) {
                buffer.append(inputLine);
            }
            String jsonStr = buffer.toString();
            ObjectMapper mapper = new ObjectMapper();
            ReturnResult returnResult = mapper.readValue(jsonStr, ReturnResult.class);
            if (returnResult.resultcode.equals("200")) {
                return jsonStr;
            }
        } catch (Exception ex) {
            //ex.printStackTrace();
            System.out.println("手机所属地查询失败====================");
        }

        return "unknown";
    }

    public static boolean isValid(String mobile) {
        return mobile.matches("^(13|15|18)\\d{9}$") || mobile.matches("^(013|015|018)\\d{9}$");
    }

    class ReturnResult {
        private String resultcode;
        private String reason;
        private int error_code;
        private Result result;

        public String getResultcode() {
            return resultcode;
        }

        public void setResultcode(String resultcode) {
            this.resultcode = resultcode;
        }

        public String getReason() {
            return reason;
        }

        public void setReason(String reason) {
            this.reason = reason;
        }

        public int getError_code() {
            return error_code;
        }

        public void setError_code(int error_code) {
            this.error_code = error_code;
        }

        public Result getResult() {
            return result;
        }

        public void setResult(Result result) {
            this.result = result;
        }
    }

    class Result {
        private String province;
        private String city;
        private String areacode;
        private String zip;
        private String company;
        private String card;

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getAreacode() {
            return areacode;
        }

        public void setAreacode(String areacode) {
            this.areacode = areacode;
        }

        public String getZip() {
            return zip;
        }

        public void setZip(String zip) {
            this.zip = zip;
        }

        public String getCompany() {
            return company;
        }

        public void setCompany(String company) {
            this.company = company;
        }

        public String getCard() {
            return card;
        }

        public void setCard(String card) {
            this.card = card;
        }
    }
}
